package com.weizhi.server.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Cleanup;
import lombok.SneakyThrows;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;

public class JsonUtil {
    @SneakyThrows
    public static JSONObject convertRequest(HttpServletRequest req){
        String contentType = req.getContentType();
        if(!contentType.startsWith("application/json")) throw new RuntimeException("请发送json格式的请求");

        StringBuilder sb = new StringBuilder();
        @Cleanup
        BufferedReader reader = req.getReader();
        String line = null;
        while((line = reader.readLine()) != null){
            sb.append(line);
        }
        return JSON.parseObject(sb.toString());
    }
}
